/**
 * Created by MD-3 on 2017/5/25.
 */

//输入提示
function searchAddress(){
    var keyword = document.getElementById("keyword").value;
    console.log(keyword);

    var flag = myDeviceSearcher(keyword);
    if(!flag){
        AMap.service(["AMap.Geocoder"], function() {
            var geocoder = new AMap.Geocoder({
                radius: 1000 //范围，默认：500
            });
            //地理编码,返回地理编码结果
            geocoder.getLocation(keyword, function(status, result) {
                if (status === 'complete' && result.info === 'OK') {
                    geocoder_CallBack(result);
                }
            });
        });
    }
}

//地理编码返回结果展示
function geocoder_CallBack(data) {
    var resultStr = "";
    //地理编码结果数组
    var geocode = data.geocodes;
    var lng, lat;
    var zoomLevel = 10;
    for (var i = 0; i < geocode.length; i++) {
        //拼接输出
        lng = geocode[i].location.getLng();
        lat = geocode[i].location.getLat();
        console.log("<地址>：" + geocode[i].formattedAddress + "----" + "<坐标>：" + geocode[i].location.getLng() + ", " + geocode[i].location.getLat() + "----" + "<匹配级别>：" + geocode[i].level)
    }
    map.setZoomAndCenter(zoomLevel,[lng,lat]);
    //map.setFitView();            //根据地图上添加的覆盖物分布情况，自动缩放地图到合适的视野级别
}

function myDeviceSearcher(keyword) {
    var device = getDeviceByName(keyword);
    if(device && device.marker){
        var zoomLevel = initmap_mock.centerZoom;
        var lng = device.marker.getPosition().getLng();
        var lat = device.marker.getPosition().getLat();
        map.setZoomAndCenter(zoomLevel,[lng,lat]);
        return true;
    }
    return false;
}